//
//  PollingDetailViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/5.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingDetailViewController.h"
#import "PollingProgressView.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import "CInputBoxView.h"
#import "PollingDetailView.h"
#import "PollingIdeaViewController.h"
#import "PollingTableViewController.h"
#import "PollingPhoneView.h"
#import "CreateProduceViewController.h"
#import "PollingTableViewController.h"
#import "PollingCompleteTableViewController.h"
#import "UnusualPollingViewController.h"
#import "IdeaCompleteViewController.h"
#import "EditProduceViewController.h"
#import "LoadingView.h"
@interface PollingDetailViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BMKLocationService* _locService;
    BMKGeoCodeSearch *_geocodesearch;
    BOOL isGeoSearch;
    //发送本地定位
    double longitude;
    double latitude;
    BOOL isAddInfo;
    
    
}
@property(nonatomic,strong)UIButton *bottomBtn;

@property(nonatomic,strong)UIView*rightView;
@property(nonatomic,strong)PollingDetailView*detailTable;
@property(nonatomic,strong) UIView *detailView;
@property(nonatomic,strong)PollingProgressView* progressView;
@property(nonatomic,strong)UIView *proView;

@end

@implementation PollingDetailViewController
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.detailTable refrshData];
//    [self.progressView refresh];

}
- (void)initView
{
    [super initView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startPolling:) name:@"startPolling" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(acceptPolling:) name:@"acceptPolling" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(createPolling:) name:@"createPolling" object:nil];

    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    //编码服务的初始化(就是获取经纬度,或者获取地理位置服务)
    _geocodesearch.delegate = self;//设置代理为self
    
    self.navBar.hidden=NO;
    __block typeof(self) blockself = self;
    
    NSInteger num =[self.state integerValue];
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    [narView resignFirstResponder];
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        [blockself backController];
        
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 50 - imgBack.frame.size.width,44);
    
    [backTitle setText:@"返回"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    _rightView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-80, 0, 80, 64)];
    [narView addSubview:_rightView];

    UILabel *rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 65, 44)];
    rightLab.text =@"转介";
    rightLab.textAlignment=NSTextAlignmentRight;
    rightLab.font=F(14);
    rightLab.textColor=MainColor;
    rightLab.userInteractionEnabled=YES;
    [_rightView addSubview:rightLab];
    [_rightView bk_whenTapped:^{
        
        //转介
        [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
            if (index==0) {
                [self setIsLoading:YES];
                
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                [dic setObject:self.warningUuid forKey:@"uuid"];
                [dic setObject:boxView.inputData forKey:@"content"];
                [CClass HttpRequestTask:YES withUrl:@"inspection_transfer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                    [CClass showLittleTimeMessage:@"转介成功" completion:^{
                        [self backController];
                        [self setIsLoading:NO];
                        
                    }];
                    
                } onError:^(BaseModel *model) {
                    [self setIsLoading:NO];

                }];
            }
        }];

        
        
    }];
    UIView *topView =[[UIView alloc]init];
    topView.frame=CGRectMake((WindowWidth-140)/2, 30, 140, 25);
    
    topView.layer.masksToBounds=YES;
    topView.layer.cornerRadius=4;
    topView.backgroundColor =C(@"ebe9e5");
    [narView addSubview:topView];
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame =CGRectMake(0, 0, topView.bounds.size.width/2,  topView.bounds.size.height);
    [btn1 setTitle:@"进度" forState:UIControlStateNormal];
    btn1.titleLabel.font =F(14);
    [topView addSubview:btn1];
    [btn1 setBackgroundColor:MainColor];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame =CGRectMake(topView.bounds.size.width/2, 0, topView.bounds.size.width/2,  topView.bounds.size.height);
    [btn2 setTitle:@"详情" forState:UIControlStateNormal];
    btn2.titleLabel.font =F(14);
    [topView addSubview:btn2];
    [btn2 setBackgroundColor:C(@"ebe9e5")];
    [btn2 setTitleColor:C(@"a7a6a6") forState:UIControlStateNormal];
    
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, narView.bounds.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
    [bottomLineView setBackgroundColor:LineColor];
    [narView addSubview:bottomLineView];
    __block typeof (self)blockSelf=self;

    self.detailView=[[UIView alloc]initWithFrame:CGRectMake(0, 64,WindowWidth,  WindowHeight-64)];
    [self.view addSubview:self.detailView];
    self.detailView.backgroundColor=BackColor;
    self.detailTable =[[PollingDetailView alloc]initWithFrame:CGRectMake(0, 0,WindowWidth,  self.detailView.bounds.size.height) withWarningUuid:self.warningUuid];
    [self.detailView addSubview: self.detailTable];
#pragma mark进入可以编辑
    [self.detailTable setGotoTableEdit:^(NSString *stateIdea,NSMutableDictionary *dataDic,NSMutableArray *dataAr) {
        //is_completed 0 为没有填完 1为填完
            PollingTableViewController *networkVC=[[PollingTableViewController alloc]init];
            networkVC.passDic=dataDic;
            networkVC.allAr=dataAr;
            networkVC.stateIdea=stateIdea;
            networkVC.Uuid=blockSelf.warningUuid;
            [blockSelf pushController:networkVC];
    
     
    }];

#pragma mark 进入可以编辑的建议
    [self.detailTable setGotoIdeaEdit:^(NSString *stateIdea, NSMutableDictionary *dataDic, NSMutableArray *dataAr) {
        
        //stateIdea  为没有填完 1为填完
    
            PollingIdeaViewController *ideaVC=[[PollingIdeaViewController alloc]init];
            if([stateIdea isEqualToString:@"0"])
            {
                [ideaVC setType:IdeaEdit];
                
            }else
            {
                [ideaVC setType:IdeaDetail];
                
            }
            ideaVC.passDic=dataDic;
            ideaVC.allAr=dataAr;
            ideaVC.Uuid=blockSelf.warningUuid;
            [blockSelf pushController:ideaVC];

        
    }];
#pragma mark进入不可编辑的表格
    [self.detailTable setGotoTableComplete:^(NSString*unusualNum, NSMutableDictionary *dataDic, NSMutableArray *dataAr) {
        //unusualNum;//有异常为1没有异常为0
        PollingCompleteTableViewController *pollingVC=[[PollingCompleteTableViewController alloc]init];
        pollingVC.passDic=dataDic;
        pollingVC.allAr=dataAr;
        pollingVC.unusalState=unusualNum;
        pollingVC.Uuid=blockSelf.warningUuid;
        [blockSelf pushController:pollingVC];
        
    }];
#pragma mark 进入不可编辑建议的界面
    [self.detailTable setGotoIdeaComplete:^(NSString*unusualNum, NSMutableDictionary *dataDic, NSMutableArray *dataAr) {
        //unusualNum;//有异常为1没有异常为0
        IdeaCompleteViewController *ideaCompleteVC=[[IdeaCompleteViewController alloc]init];
        ideaCompleteVC.passDic=dataDic;
        ideaCompleteVC.allAr=dataAr;
        ideaCompleteVC.unusualState=unusualNum;
        ideaCompleteVC.Uuid=blockSelf.warningUuid;
        [blockSelf pushController:ideaCompleteVC];
    }];
    
#pragma mark 进入异常汇总
    [self.detailTable setGotoUnuaual:^(NSString *unusualNum, NSMutableDictionary *dataDic, NSMutableArray *dataAr) {
        
        UnusualPollingViewController *unusualVC=[[UnusualPollingViewController alloc]init];
        unusualVC.passDic=dataDic;
        unusualVC.allAr=dataAr;
        unusualVC.unusualState=unusualNum;
        unusualVC.Uuid=blockSelf.warningUuid;
        [blockSelf pushController:unusualVC];
        
    }];
#pragma mark  生成报告
    [self.detailTable setGotoProduce:^(NSMutableDictionary *dict) {
        NSString *stateNum=[dict objectForKey:@"stateNum"];//11退回 10 处理中
        if ([stateNum isEqualToString:@"11"]) {
            
            EditProduceViewController *editVC=[[EditProduceViewController alloc]init];
            editVC.mobileAr=[dict objectForKey:@"moblie"];
            editVC.Uuid=[dict objectForKey:@"Uuid"];
            editVC.isHasPhone=@"1";
            editVC.isHiddleEdit=YES;
            [blockSelf pushController:editVC];
        }else
        {
            CreateProduceViewController *produceVC=[[CreateProduceViewController alloc]init];
            produceVC.mobileAr=(NSMutableArray*)[dict objectForKey:@"moblie"];
            produceVC.Uuid=[dict objectForKey:@"Uuid"];
            [blockSelf pushController:produceVC];
        }
        
        
    }];
    _proView=[[UIView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    [self.view addSubview:_proView];
    _proView.backgroundColor =[UIColor clearColor];
    _progressView=[[PollingProgressView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, _proView.frame.size.height-10)withWarningUuid:self.warningUuid];
    [_proView addSubview:_progressView];
    

//    _bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, WindowHeight-70, WindowWidth, 70)];
//    _bottomView.backgroundColor=BackColor;
//    [self.view addSubview:_bottomView];
   _bottomBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_bottomBtn];
    _bottomBtn.frame=CGRectMake(29, WindowHeight-40-15, WindowWidth-58, 40);
    [_bottomBtn setBackgroundColor:MainColor];
    _bottomBtn.layer.masksToBounds=YES;
    _bottomBtn.layer.cornerRadius=8;
    if (self.type==1) {
        //开始处理后巡检单不能再被转介
        if (num==0||num==1||num==2) {
            _rightView.hidden=NO;
            
        }else
        {
            _rightView.hidden=YES;
            
            
        }
        self.detailView.hidden=YES;
        _bottomBtn.hidden=NO;
        [btn1 setBackgroundColor:MainColor];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn2.backgroundColor=C(@"e8e8e8");
        [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
        if (num==1) {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74-70);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            _bottomBtn.hidden=NO;
            
            
        }else
        {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            _bottomBtn.hidden=YES;
            
            
        }
    }else if(self.type==2)
    {
        _rightView.hidden=YES;
        _bottomBtn.hidden=YES;
        self.detailView.hidden=NO;
        _proView.hidden=YES;
        [btn2 setBackgroundColor:MainColor];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.backgroundColor=C(@"e8e8e8");
        [btn1 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    }
    

    [btn1 bk_addEventHandler:^(id sender) {
        //开始处理后巡检单不能再被转介
        if (num==0||num==1||num==2) {
            _rightView.hidden=NO;
            
        }else
        {
            _rightView.hidden=YES;
            
            
        }
        if (num==1) {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74-70);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            _bottomBtn.hidden=NO;
            
            
        }else
        {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            _bottomBtn.hidden=YES;
            
            
        }
      
        [self.progressView refresh];
        _proView.hidden=NO;
        self.detailView.hidden=YES;
        [btn1 setBackgroundColor:MainColor];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn2.backgroundColor=C(@"e8e8e8");
        [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [btn2 bk_addEventHandler:^(id sender) {
        _rightView.hidden=YES;
        _bottomBtn.hidden=YES;
        self.detailView.hidden=NO;
        _proView.hidden=YES;
        [btn2 setBackgroundColor:MainColor];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.backgroundColor=C(@"e8e8e8");
        [btn1 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    } forControlEvents:UIControlEventTouchUpInside];
    
    
  
    
    if (num==0) {
        //待分配 0 不可能出现
        
    }else if(num==1)
    {
        //  待接单 1
      
        [_bottomBtn setTitle:@"接单" forState:UIControlStateNormal];
        [_bottomBtn bk_addEventHandler:^(id sender) {
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setObject:self.warningUuid forKey:@"uuid"];
          
            [[LoadingView shareLoading] startAnimating];
            [CClass HttpRequestTask:YES withUrl:@"inspection_accept" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                [[LoadingView shareLoading] stopAnimating];
        
                [CClass showLittleTimeMessage:@"接单成功" completion:nil];
                [self backController];

                
                
            } onError:^(BaseModel *model) {
                [[LoadingView shareLoading] stopAnimating];
 
            }];
       
           

        } forControlEvents:UIControlEventTouchUpInside];
       
        
        
    }
}


  //接单之后
- (void)acceptPolling:(NSNotification*)noti
{
    //接单之后
    [self.progressView refresh];
    [_bottomBtn removeFromSuperview];
    
}
- (void)startPolling:(NSNotification*)noti
{
    //开始巡检之后
    [self.progressView refresh];
//    _bottomBtn.hidden=YES;
//    _rightView.hidden=YES;
    [_bottomBtn removeFromSuperview];
    [_rightView removeFromSuperview];

    
}
- (void)createPolling:(NSNotification*)noti
{
    //生成报告之后
    [self.progressView refresh];
    [self.detailTable setStateNum:20];
//    _bottomBtn.hidden=YES;
//    _rightView.hidden=YES;
    [_bottomBtn removeFromSuperview];
    [_rightView removeFromSuperview];
    
}


@end
